package handler

import (
	"errors"

	"github.com/gin-gonic/gin"
	"github.com/go-xorm/xorm"

	"gitee.com/goframe/lt-collector/libs/libgin"
	"gitee.com/goframe/lt-collector/libs/orma"
	"gitee.com/goframe/lt-collector/model"
)

var GameHandler Game

type (
	Game struct{}
)

//获取采集源类型
//API
func (h Game) GetSources() gin.HandlerFunc {
	return libgin.Return.WarpHandler(func(c *gin.Context) (err error) {
		libgin.Return.GinOK(c, model.Sources)
		return
	})
}

//获取彩种当日开奖时间表
func (h Game) OpenTimeList() gin.HandlerFunc {
	return libgin.Return.WarpHandler(func(c *gin.Context) (err error) {
		gid := c.Param("gid")
		plan := new(model.GameIssuePlan)
		err = orma.NormalGet(plan.Session().Where("gid=?", gid).Get(plan))
		if err != nil {
			return
		}
		libgin.Return.GinOK(c, plan.OpenTimeList())
		return
	})
}

//OpenInfo 开奖信息
func (h Game) OpenInfo() libgin.WarpGinHandler {
	return func(c *gin.Context) (err error) {
		var openData *model.OpenData
		gid := c.Param("gid")
		lt := new(model.GameLotteries)
		err = orma.NormalGet(lt.Session().Where("id=? or `name`=?", gid, gid).Get(lt))
		if err != nil {
			return
		}
		openData, err = lt.OpenInfo()
		libgin.Return.GinOK(c, openData)
		return
	}
}

//AddLotto 添加彩种
func (h Game) AddLotto() libgin.WarpGinHandler {
	//接口结构
	type Bind struct {
		//彩种类型ID
		TypeID int `json:"type_id" binding:"required"`
		//彩种英文名
		Name string `json:"name" binding:"required"`
		//彩种中文名
		NameCN string `json:"name_cn" binding:"required"`
		IsSys  bool   `json:"is_sys"`
		//开奖时间计划
		TimePlans []model.OpenTimePlan `json:"time_plans" binding:"required"`
		//是否期号+1
		IssueAddOne    bool   `json:"issue_add_one"`
		FirstIssueTime string `json:"first_issue_time"`
		LastIssueTime  string `json:"last_issue_time"`
		//期号时+1类型的彩种需要参数
		GameIssueCheck   model.GameIssueCheck `json:"game_issue_check"`
		CollectSourceIds []int                `json:"collect_source_ids" binding:"required"`
		//开奖时间校对,每天n期
		IssueLenOneDay int `json:"issue_len_one_day" binding:"required"`
	}
	return func(c *gin.Context) (err error) {
		bindBean := new(Bind)
		if err = c.ShouldBind(bindBean); err != nil {
			return
		}
		//校对时间计划
		{
			checkPlan := new(model.OpenTimePlan)
			if err = checkPlan.CheckTimePlan(bindBean.IssueLenOneDay, bindBean.TimePlans); err != nil {
				return
			}
		}
		lt := &model.GameLotteries{
			TypeID:           bindBean.TypeID,
			Name:             bindBean.Name,
			NameCN:           bindBean.NameCN,
			Enable:           true,
			IsSys:            bindBean.IsSys,
			CollectSourceIds: bindBean.CollectSourceIds,
		}
		err = orma.SnCallBack(func(bsn *xorm.Session) error {
			var err error
			if err = orma.NormalInsert(bsn.InsertOne(lt)); err != nil {
				return err
			}
			if bindBean.IssueAddOne {
				check := &model.GameIssueCheck{
					Gid:      lt.ID,
					Issue:    bindBean.GameIssueCheck.Issue,
					OpenTime: bindBean.GameIssueCheck.OpenTime,
				}
				if len(check.Issue) == 0 {
					return errors.New("校准期号错误")
				}
				if check.OpenTime.Convert2Time().IsZero() {
					return errors.New("校准时间错误")
				}
				if err = orma.NormalInsert(bsn.InsertOne(check)); err != nil {
					return err
				}
			}
			plan := &model.GameIssuePlan{
				Gid:            lt.ID,
				TimePlans:      bindBean.TimePlans,
				IssueAddOne:    bindBean.IssueAddOne,
				FirstIssueTime: bindBean.FirstIssueTime,
				LastIssueTime:  bindBean.LastIssueTime,
			}
			if err = orma.NormalInsert(bsn.InsertOne(plan)); err != nil {
				return err
			}
			return nil
		})
		if err != nil {
			return
		}
		libgin.Return.GinOK(c, lt)
		return
	}
}
